/**
 * 
 */
package com.example.gensurdatviwi.application.datamodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 1121880
 *
 */
public class SurveyDatabaseFieldList implements Serializable {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3942356156270634964L;
	
	
	
	private List<SurveyDatabaseField> list = null;

	
	/**
	 * todo
	 * 
	 */
	public SurveyDatabaseFieldList(){
		
		this.list = new ArrayList<SurveyDatabaseField>();
		
	}
	
	
	
	/**
	 * @return the list
	 */
	public List<SurveyDatabaseField> getList() {
		return list;
	}

	
	/**
	 * @param list the list to set
	 */
	public void setList(List<SurveyDatabaseField> list) {
		this.list = list;
	}
	
	/**
	 * Add a new field to the list. 
	 * 
	 * @param field
	 */
	public void addDatabaseField(SurveyDatabaseField  field){
		
		this.list.add(field); //Noch überlegen was mit dem id Feld ist.
		
	}
	
	
	/**
	 * Add a new field to the list at a given position. 
	 * 
	 * @param field
	 */
	public void addDatabaseFieldAtPosition(SurveyDatabaseField  field, Long position){
		
		/**
		try {
			this.list.add(position.intValue(), field);
		//If we receive an too high index we add the field at the end of the collection.
		//The reason for this is the last drop field that has an too high index.
		} catch (IndexOutOfBoundsException e) {
			this.list.add(field);
		}
		**/

		
		this.list.add(position.intValue(), field);

		
	}

	
	/**
	 * Moves a field in the list from the given position to the given position. 
	 * 
	 * @param field
	 */
	public void moveDatabaseFieldFromPositionToPosition(Long startPosition, Long targetPosition){
		
		//Noch überprüfen ob wohl sicher mit 0 und 1 alles in Ordnung ist ...
		
		System.out.println("startPosition: " + startPosition + " " + "targetPosition: " + targetPosition);
		
		this.list.add(targetPosition.intValue(), this.list.get(startPosition.intValue()));
		
		if ( startPosition < targetPosition ){
			this.list.remove( startPosition.intValue() );
		}
		else {
			this.list.remove( startPosition.intValue() + 1 );			
		}
		
		
	}	



	
	
}
